package com.danco.training.zaneuskaya.connection.dao.impl;


import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.danco.training.zaneuskaya.connection.dao.AbstractHibernateDao;
import com.danco.training.zaneuskaya.connection.dao.ClaimDao;
import com.danco.training.zaneuskaya.domain.Book;
import com.danco.training.zaneuskaya.domain.Claim;


public class ClaimDaoImpl extends AbstractHibernateDao<Claim,Integer> implements ClaimDao{
	
	
	private static final Logger logger = Logger
			.getLogger(ClaimDaoImpl.class);

	
	
	public List<Claim> readClaimsByAlphabet(Session session) {
		Criteria criteria = session.createCriteria(Claim.class);
		Criteria bookCriteria = criteria.createCriteria("book");	
		bookCriteria.addOrder(Order.asc("title"));
		return criteria.list();
	}

	@Override
	public List<Claim> readClaimsByClaimAmount(Session session) {
		Criteria criteria = session.createCriteria(Claim.class);
		criteria.addOrder(Order.asc("claimAmount"));
		return criteria.list();
	}

	
	
	@Override
	public boolean executeClaim(Session session, Claim object) {
		
		object.setExecuted(true);
	
			update(session, object);
			return true;
		
			
	}

	@Override
	public boolean deleteClaimByBook(Session session, Book book) {
		for (Claim claim: findClaimByBook(session, book)){
			session.delete(claim);
		}
		return true;
		
		
	}

	@Override
	public List<Claim> findClaimByBook(Session session, Book book) {
		Criteria criteria = session.createCriteria(Claim.class);	
		criteria.add(Restrictions.eq("book", book));
		return criteria.list();
	}

	@Override
	public Claim read(Session session, Integer PK)
			throws HibernateException {
		
		return (Claim) super.read(session, Claim.class, PK);
	}

	@Override
	public List<Claim> readAll(Session session) throws HibernateException {
		
		return super.readAll(Claim.class, session);
	}


	
	
}
